﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class AC_ActivityTypes : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        ActivityTypeBLL AcBLL = new ActivityTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
            }
        }

        private void LoadDataGrid()
        {
            DataSet ListDataCommand = AcBLL.GetDataAcType("");
            DataTable table = ListDataCommand.Tables[0];
            RadGrid1.DataSource = ListDataCommand.Tables[0];
            RadGrid1.DataBind();
           
        }

        protected void RadGrid1_ItemCommand(object o, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                UpdateUI("AddNew");

            }
            else if (e.CommandName == "DataEdit")
            {
                string Sequence = RadGrid1.Items[e.Item.ItemIndex]["Sequence"].Text;
                DataSet ListDataCommand = AcBLL.GetDataAcType(Sequence);
                DataTable table = ListDataCommand.Tables[0];
                if (table.Rows.Count > 0)
                {
                    txtActivityTypeSeq.Text = table.Rows[0]["Sequence"].ToString();
                    txtActivityType.Text = table.Rows[0]["Name"].ToString();
                    txtRemark.Text = table.Rows[0]["remark"].ToString();
                }
                UpdateUI("Update");
            }
            else if (e.CommandName == "Delete")
            {
                string Sequence = RadGrid1.Items[e.Item.ItemIndex]["Sequence"].Text;
                AcBLL.Delete(Sequence, ssMng.EmpSeq);
                UpdateUI("Save");
                
            }
            LoadDataGrid();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            AcBLL.Insert(txtActivityType.Text, txtRemark.Text, ssMng.EmpSeq);
            UpdateUI("Save");
            ClearControl();

            LoadDataGrid();
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            AcBLL.Update(txtActivityTypeSeq.Text, txtActivityType.Text, txtRemark.Text, ssMng.EmpSeq);
            UpdateUI("Save");
            ClearControl();

            LoadDataGrid();
        }
        
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Cancel");
            ClearControl();
        }
        protected void ClearControl()
        {
            lblError.Text = "";
            txtActivityType.Text = "";
            txtActivityTypeSeq.Text = "";
            txtRemark.Text = "";
        }
        
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnUpdate.Visible = false;
                    btnSave.Visible = true;

                    break;
                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnSave.Visible = false;
                    btnUpdate.Visible = true;

                    break;
            }
        }

    }
}
